package com.lsyw.intergrate.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.StringUtil;
import com.lsyw.intergrate.entity.*;
import com.lsyw.intergrate.mapper.*;
import com.lsyw.intergrate.service.T0200Service;
import com.lsyw.intergrate.service.TransportWorkOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输工单信息 服务实现类
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-29
 */
@Service
public class TransportWorkOrderServiceImpl extends ServiceImpl<TransportWorkOrderMapper, TransportWorkOrder> implements TransportWorkOrderService {

    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private MassifUserMapper massifUserMapper;

    @Autowired
    private TransportTaskMapper transportTaskMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private T0200Service t0200Service;

    @Override
    public IPage<TransportWorkOrder> pageAppList(Page<TransportWorkOrder> page, Map<String, Object> condition) {
        IPage<TransportWorkOrder> iPage =baseMapper.pageAppList(page, condition);
        iPage.getRecords().forEach(e->{
            if (!StringUtil.isEmpty(e.getVehicleId())){
                e.setVehicle(vehicleMapper.selectById(e.getVehicleId()));
            }
        });
        return iPage;
    }


    @Override
    public TransportWorkOrder getWorkOrderDetail(String workOrderId) {
        TransportWorkOrder transportWorkOrder =baseMapper.getWorkOrderDetail(workOrderId);
        TransportTask task = transportTaskMapper.selectById(transportWorkOrder.getTransportTaskId());
        Route route = this.routeMapper.selectById(task.getRouteId());
        String gpsSoilId = transportWorkOrder.getGpsSoilId();
        String gpsVehicleId = transportWorkOrder.getGpsVehicleId();
        String cctvIds = transportWorkOrder.getCctvIds();

        //如果没值
        if (StringUtils.hasLength(cctvIds)){
            List<Device> deviceList=new ArrayList<>();
            List<String>cctvs= Arrays.asList(cctvIds.split(","));
            cctvs.forEach(e->{
                deviceList.add(deviceMapper.selectById(e));
            });
            transportWorkOrder.setCctvList(deviceList);
        }
        //如果没值
        if (StringUtils.hasLength(gpsSoilId)){
            List<List<Object>> soilRealRoute=new ArrayList<>();
            LambdaQueryWrapper<T0200> lambda = new QueryWrapper<T0200>().lambda();
            lambda.eq(T0200::getNo ,gpsSoilId);
            lambda.eq(T0200::getOrderId ,workOrderId);
            lambda.orderByAsc(T0200::getGmtCreate);
            List<T0200> list = this.t0200Service.list(lambda);
            list.forEach(e->{
                List<Object> list1=new ArrayList<>();
                list1.add(e.getLongitude());
                list1.add(e.getLatitude());
                soilRealRoute.add(list1);
            });
            transportWorkOrder.setSoilRealRoute(soilRealRoute);
        }
        if (StringUtils.hasLength(gpsVehicleId)){
            List<List<Object>> vehicleRealRoute=new ArrayList<>();
            LambdaQueryWrapper<T0200> lambda = new QueryWrapper<T0200>().lambda();
            lambda.eq(T0200::getNo ,gpsVehicleId);
            lambda.eq(T0200::getOrderId ,workOrderId);
            lambda.orderByAsc(T0200::getGmtCreate);
            List<T0200> list = this.t0200Service.list(lambda);
            list.forEach(e->{
                List<Object> list1=new ArrayList<>();
                list1.add(e.getLongitude());
                list1.add(e.getLatitude());
                vehicleRealRoute.add(list1);
            });
            transportWorkOrder.setVehicleRealRoute(vehicleRealRoute);
        }
        transportWorkOrder.setRoute(route);
        transportWorkOrder.setVehicle(vehicleMapper.selectById(transportWorkOrder.getVehicleId()));
        transportWorkOrder.setDriver(massifUserMapper.selectById(transportWorkOrder.getDriverId()));

        String companyId = task.getCompanyId();
        Company company = this.companyMapper.selectById(companyId);
        String jsCompanyId = task.getJsCompanyId();
        if (!StringUtil.isEmpty(jsCompanyId)){
            Company jsCompany = this.companyMapper.selectById(jsCompanyId);
            transportWorkOrder.setJsCompanyName(jsCompany.getName());
        }

        transportWorkOrder.setCompany(company);
        transportWorkOrder.setTask(task);
        return transportWorkOrder;
    }

    @Override
    public List<Map<String, Object>> getValueByDqy(Map<String, Object> map) {
        return baseMapper.getValueByDqy(map);
    }
}
